package com.wanchuang.qhbb.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import androidx.annotation.NonNull;

import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.utils.DateUtil;
import com.wanchuang.qhbb.utils.DensityUtil;
import com.ruffian.library.widget.RTextView;
import com.ycuwq.datepicker.date.DayPicker;

import java.util.ArrayList;
import java.util.List;

import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;
import kale.ui.view.dialog.EasyDialog;

public class SelectDateDialog extends BaseCustomDialog {
    public OnDialogClickListener dialogListener;
    public DayPicker dayPicker;
    private RTextView tvSure;
    private RTextView tvCancel;


    // 自定义builder来增加一些参数，记得要继承自BaseEasyDialog.Builder
    public static class Builder extends BaseEasyDialog.Builder<Builder> {
        private Bundle bundle = new Bundle();
        private OnDialogClickListener listener;

        public Builder(@NonNull Context context) {
            super(context);
        }


        public Builder setOnDialogClickListener(OnDialogClickListener lis) {
            listener = lis;
            return this;
        }

        public SelectDateDialog.Builder setSelectTime(String year, String month, String day) {
            bundle.putString("year", year);
            bundle.putString("month", month);
            bundle.putString("day", day);
            return this;
        }

        @NonNull
        @Override
        protected SelectDateDialog createDialog() {
            SelectDateDialog dialog = new SelectDateDialog();
            dialog.setArguments(bundle);
            dialog.dialogListener = listener;
            return dialog;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_select_date;
    }

    @Override
    protected void bindViews(View view) {
        dayPicker = findView(R.id.day_picker);

        int set_year = Integer.parseInt(getArguments().getString("year"));
        int set_month = Integer.parseInt(getArguments().getString("month"));
        dayPicker.setMonth(set_year, set_month);
        Log.e("year", set_year + "");
        Log.e("month", set_month + "");
        Log.e("DateUtilyear", DateUtil.getYear() + "");
        Log.e("DateUtilmonth", DateUtil.getMonth() + "");
        if (set_year == DateUtil.getYear() && set_month == DateUtil.getMonth()) {
            List<Integer> list = new ArrayList<>();
            for (int i = 1; i <= DateUtil.getDay(); i++) {
                list.add(i);
            }
            dayPicker.setDataList(list);
        }
        dayPicker.setSelectedDay(Integer.parseInt(getArguments().getString("day")));
        tvCancel = findView(R.id.tv_cancel);
        tvSure = findView(R.id.tv_sure);
        setListener();
    }

    private void setListener() {
        tvSure.setOnClickListener(view -> {
            int day = dayPicker.getSelectedDay();
            dialogListener.onClick(view, this, String.format("%02d", day));
        });
        tvCancel.setOnClickListener(view -> SelectDateDialog.this.dismiss());
    }

    @Override
    protected void setViews() {
        setBackground();
        setLayout();
    }

    //     强制取消背景，保持有透明
    private void setBackground() {
        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.white_bg); // 设置背景
    }

    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        // 强制宽高
        int padding = DensityUtil.dp2px(getContext(), 40);
        lp.gravity = Gravity.CENTER; // 设置展示的位置
        window.setAttributes(lp);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    //  监听输入框内容
    public interface OnDialogClickListener {

        void onClick(View v, EasyDialog dialog, String day);
    }
}
